package com.laivi.basic.model.file.reader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


/**
 * @author laivi.zhu@gmail.com
 * @date 2014-7-9
 * description:
 */
public class XlsxReader implements IExcelReader {
	private Workbook book;
	private Sheet sheet;
	
	public XlsxReader(String path){
		this(new File(path));
	}
	
	public XlsxReader(File file){
		try {
			book=new XSSFWorkbook(new FileInputStream(file));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setSheet(int index) {
		sheet=book.getSheetAt(index);
	}

	@Override
	public String getContent(int row, int col) {
		sheet.getRow(row).getCell(col).setCellType(Cell.CELL_TYPE_STRING);
		return sheet.getRow(row).getCell(col).getStringCellValue();
	}

	@Override
	public void close() {
		
	}

	@Override
	public int getRowNum() {
		return sheet.getLastRowNum();
	}

}
